# gozerbot/plugs/userstate.py
#
#

""" userstate is stored in gozerdata/userstates. """

# gozerlib imports

from gozerlib.commands import cmnds
from gozerlib.examples import examples
from gozerlib.persist.persiststate import UserState
from gozerlib.users import GozerUsers
from gozerlib.aliases import aliasset

def handle_userstate(bot, ievent):

    """ let the user manage its own state. """

    try:
        (item, value) = ievent.args
    except ValueError:
        item = value = None

    users = GozerUsers()
    username = users.getname(ievent.userhost)
    userstate = UserState(username)
    if item and value:
        userstate[item] = value
        userstate.save()

    result = []
    for i, j in userstate.data.iteritems():
        result.append("%s=%s" % (i, j))

    if result:
        ievent.reply("userstate of %s: " % username, result, dot=True)
    else:
        ievent.reply('no userstate of %s known' % username)

cmnds.add('userstate', handle_userstate, 'USER')
examples.add('userstate', 'get or set userstate', '1) userstate 2) userstate TZ -1')
aliasset('set', 'userstate')

def handle_userstateget(bot, ievent):

    """ get state of a user. """

    if not ievent.rest:
        ievent.missing('<username>')
        return

    userstate = UserState(ievent.rest)
    result = []
    for i, j in userstate.data.iteritems():
        result.append("%s=%s" % (i, j))

    if result:
        ievent.reply("userstate of %s: " % ievent.rest, result, dot=True)
    else:
        ievent.reply('no userstate of %s known' % ievent.rest)

cmnds.add('userstate-get', handle_userstateget, 'OPER')
examples.add('userstate-get', 'get the userstate of another user', 'userstate-get dunker')

def handle_userstateset(bot, ievent):

    """ set the userstate of a user. """

    try:
        (username, item, value) = ievent.args
    except ValueError:
        ievent.missing('<username> <item> <value>')
        return

    userstate = UserState(username)
    userstate[item] = value
    userstate.save()
    ievent.reply('userstate %s set to %s' % (item, value))

cmnds.add('userstate-set', handle_userstateset, 'OPER')
examples.add('userstate-set', 'set userstate variable of another user', 'userstate-set dunker TZ -1')

def handle_userstatedel(bot, ievent):

    """ remove value from user state. """

    username = None
    try:
        (username, item)  = ievent.args
    except ValueError:
        try:
           item = ievent.args[0]
        except IndexError:
            ievent.missing('[username] <item>')
            return

    if not username:
        users = GozerUsers()
        username = users.getname(ievent.userhost)

    userstate = UserState(username)
    try:
        del userstate.data[item]
    except KeyError:
        ievent.reply('no such item')
        return

    userstate.save()
    ievent.reply('item %s deleted' % item)

cmnds.add('userstate-del', handle_userstatedel, 'OPER')
examples.add('userstate-del', 'delete userstate variable', '1) userstate-del TZ 2) userstate-del dunker TZ')

def handle_unset(bot, ievent):

    """ remove value from user state of the user giving the command. """

    try:
        item = ievent.args[0]
    except IndexError:
        ievent.missing('<item>')
        return
 
    users = GozerUsers()
    username = users.getname(ievent.userhost)
    userstate = UserState(username)

    try:
        del userstate.data[item]
    except KeyError:
        ievent.reply('no such item')
        return

    userstate.save()
    ievent.reply('item %s deleted' % item)

cmnds.add('unset', handle_unset, 'USER')
examples.add('unset', 'delete userstate variable for user gving the command', '1) unset TZ')
